/*137
作者：肖毅(YesSky)
作品: MC.Dialog js弹出层
时间: 2009.10.12 更新2010.7.25
版本: MC.Dialog.10.7.25.js
版权: 本程序版权归作者肖毅，你可以使用本程序，但不得作为商业用途，不得以本程序盈利
      如果修改本程序必须通知本人并注明，在任何地方使用必须保留本段申明,如果违反本原则将追究相关责任
网站: http://live126.cn/
blog: http://blog.sina.com.cn/zkunet
qq:   441548727
mail: admin@esbing.cn
*/
(function() {
	this.alert = function(str) {
		Dialog.DAlert(str);
	};
	this.confirm = function(str,func1,func2) {
		Dialog.Confirm(str,func1,func2);
	};

/*	this.dalert = function(str) {
		Dialog.DAlert(str);
	};
	this.dconfirm = function(str,func1,func2) {
		Dialog.Confirm(str,func1,func2);
	};*/
	this.Event = {
		stop:function(event) {
			if(!event){return;}
			if(gecko){
				event.preventDefault();
				event.stopPropagation();
			}
			event.cancelBubble = true
			event.returnValue = false;
		},
		add:function(a,b,c){
			if(ie){
				a.attachEvent("on"+b,c);
			} else{
				a.addEventListener(b,c,false);
			}
		},
		remove:function(a,b,c){
			if(ie){
				a.detachEvent("on"+b,c);
			} else{
				a.removeEventListener(b,c,false);
			}
		}
	};
	this.$dialog = function(id) {
		return document.getElementById(id);
	};
	this.page = function(win){
		win = win || window;
		var doc = win.document;
		var cw = doc.compatMode == "BackCompat" ? doc.body.clientWidth : doc.documentElement.clientWidth;
		var ch = doc.compatMode == "BackCompat" ? doc.body.clientHeight : doc.documentElement.clientHeight;
		var sl = Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft);
		var st = Math.max(doc.documentElement.scrollTop, doc.body.scrollTop);
		var sw = Math.max(doc.documentElement.scrollWidth, doc.body.scrollWidth);
		var sh = Math.max(doc.documentElement.scrollHeight, doc.body.scrollHeight);
		var w = Math.max(sw, cw);
		var h = Math.max(sh, ch);
		return {
			"clientWidth": cw,
			"clientHeight": ch,
			"scrollLeft": sl,
			"scrollTop": st,
			"scrollWidth": sw,
			"scrollHeight": sh,
			"width": w,
			"height": h
		}
	};
	var ie = navigator.userAgent.toLowerCase().indexOf("msie") != -1,
	gecko = navigator.userAgent.toLowerCase().indexOf("gecko") != -1,
	opera = navigator.userAgent.toLowerCase().indexOf("opera") != -1,
	topwin = this.top,
	fade = function(element, start, end, speed, callback){
		//透明度渐变：start:开始透明度 0-100；end:结束透明度 0-100；speed:速度1-100
		if(!element.effect)
			element.effect = {fade:0, move:0, size:0};
		clearInterval(element.effect.fade);
		var speed=speed||20;
		element.effect.fade = setInterval(function(){
			start = start < end ? Math.min(start + speed, end) : Math.max(start - speed, end);
			element.style.opacity =  start / 100;
			element.style.filter = "alpha(opacity=" + start + ")";
			if(start == end){
				clearInterval(element.effect.fade);
				if(callback)
					callback.call(element);
			}
		}, 20);
	},
	Dialog;
	Array.prototype.each = function(fn,i) {
		for(var i=0; i<this.length; i++) {
			fn(this[i],i);
		}
		return this;
	};
	Array.prototype.index = function(el) {
		var index = -1;
		for(var i=0; i<this.length; i++) {
			if(this[i]==el) {
				index = i;break;
			}
		}
		return index;
	};
	Array.prototype.include = function(tar) {
		var flag = -1;
		this.each(function(it){
			if(it==tar) flag = 1;
		})
		return flag;
	};
	Array.prototype.remove = function(s){
		for(var i=0;i<this.length;i++){
			if(s == this[i]){
				this.splice(i, 1);
			}
		}
	};
	/*
	弹出层实现
	*/
	this.Dialog = Dialog = function(sID) {
		this.ID = sID;  //ID 标识 唯一
		this.Config = {};
		//以下是保存的内部子对象
		this.Width = 0; 
		this.Height = 0; 
		this.Title = "";  //标题内容
		this.Frame = null; //内嵌框架
		this.Message = null; //提示栏
		this.Button = null; //按钮栏
		this.Event = []; //保存每个实例所监听的事件 保证关闭时回收内存
	};
	Dialog.Queue = [];
	Dialog.Path = "";
	Dialog.Mask = null;
	Dialog.listener = function() {
		Event.add(topwin.document,"keydown",Dialog.onKeyUp);
		Event.add(topwin.document,"click",Dialog.setFocus);
		Event.add(topwin,'resize',Dialog.setPosition);
		Event.add(topwin,'scroll',Dialog.setPosition);
	};
	Dialog.prototype.init = function() {
		this.ID = this.Config.id;
		if(this.ID=="" || this.ID == undefined || this.ID == null) {
			this.ID = "mc-dialog-client-" + (+new Date);
		} else {
			if(topwin.Dialog.Queue.include(this.ID)!=-1) {
				this.ID = this.ID + '_' + (+new Date);
			}
		}
		this.Width = this.Config.width || 400;
		this.Height = this.Config.height || 200;
		this.Config.mask = this.Config.mask || false;
		this.Config.drag = this.Config.drag || false;
		this.Config.title = this.Config.title || "系统提示";
		if(!this.Config.frame) this.Config.frame = {
			mode:"text", // text or url
			url:"about:blank",
			icon:"icon_alert.gif",
			text:"这是MC Dialog 为您自动创建的内容",
			loading:false, //开启加载提示
			onload:function() {
			}
		};
		if(this.Config.frame.mode!="text" && this.Config.frame.mode!="url") this.Config.frame.mode = "text";
		if(!this.Config.message) this.Config.message = {
			show:false,
			title:"mc dialog",
			text:"提升用户体验从mc dialog开始"
		};
		if(!this.Config.button) this.Config.button = {
			show:false,
			yes:{
				show:false,
				text:"确定",
				fn:function(){}
			},
			no:{
				show:false,
				text:"取消",
				fn:function(){}
			}
		};
	};
	Dialog.prototype.create = function() {
		this.init();
		var html_ui = "\
		<table style='-moz-user-select:none;' oncontextmenu='return false'  onselectstart='return false;' width='100%' align='center' border='0' cellspacing='0' cellpadding='0'>\
		  <tr><td class='dialog_lt'></td>\
		  <td height='33' id='mc-dialog-drag-handler-"+this.ID+"' class='dialog_ct'>\
		  <div id='mc-dialog-title-"+this.ID+"' style=\"float:left;font-weight:bold; color:#FFFFFF; padding:10px 0 0 4px; font-size:12px;\">\
			<img class='icon_dialog' src='"+Dialog.Path+"icon_dialog.gif' width='13' height='13' align='absmiddle'>\
			<span id='mc-dialog-title-text-"+this.ID+"'></span>\
			</div><div id='mc-dialog-button-close-"+this.ID+"' class='dialog_closebtn'  onClick=\"Dialog.getInstance('"+this.ID+"').Button.No.onclick.apply(Dialog.getInstance('"+this.ID+"').Button.No,[]);\"></div>\
			</td><td class='dialog_rt'></td></tr>\
		    <tr><td class='dialog_mlm' ></td><td align='center' valign='top'>\
				<table id='mc-dialog-main-"+this.ID+"' width='100%' border='0' cellspacing='0' cellpadding='0'>\
					<tr id='mc-dialog-messageRow-"+this.ID+"'>\
					<td align='left' height='50' valign='middle' bgcolor='#f4f4f4' style='border-bottom:1px solid #CCCCCC; padding-left:10px;'><table cellpadding='0' cellspacing='0' width='100%' height='100%' border='0'><tr>\
					<td width='50' height='100%' align='center' valign='middle'><img src='"+Dialog.Path+"window.gif' width='32' height='32' align='absmiddle'></td><td>\
					<div id='mc-dialog-message-title-"+this.ID+"' style='vertical-align:middle;height:20px;line-height:20px;color:#333333; padding-left:10px; width:auto; font-size:13px;font-weight:600;'></div>\
					<div id='mc-dialog-message-text-"+this.ID+"' style='vertical-align:middle;height:20px; line-height:20px;color:#333333; padding-left:10px; width:auto; font-size:13px;overflow:hidden;'></div></td></tr></table></td></tr>\
					<tr><td align='left' bgcolor='#ffffff' valign='top'>\
					<div id='mc-dialog-frame-box-"+this.ID+"' style='display:none;position:absolute;width:100%;height:100%;filter:(opacity=0);opacity:0;'></div>\
					<div id='mc-dialog-frame-loading-"+this.ID+"' style='padding:10px 0 0 0; font-size:12px; text-align:center;'>载入中...</div>\
					<iframe src='' id='mc-dialog-frame-"+this.ID+"' allowTransparency='true' frameborder='0' style='background-color: #transparent;border:none;overflow-x:hidden;'></iframe>\
					</td></tr>\
					<tr id='mc-dialog-buttonRow-"+this.ID+"'><td height='40' align='right' valign='middle' bgcolor='#f4f4f4' style='border-top:1px solid #cccccc; padding-right:10px;'>\
					<div id='mc-dialog-button-list-"+this.ID+"' style='list-style:none; text-align:center; float:right;'>\
					<button id='mc-dialog-button-ok-"+this.ID+"''></button>\
					<button id='mc-dialog-button-cancel-"+this.ID+"'></button>\
				</div></td></tr></table></td><td class='dialog_mrm'></td></tr>\
			<tr><td class='dialog_lb'></td><td class='dialog_cb'></td><td class='dialog_rb'></td></tr></table>\
		";
		//创建MC Dialog容器
		var div = topwin.document.createElement("div");
		div.className = "mc-dialog-box";
		div.id = "mc-dialog-box-"+this.ID;
		div.style.position = "absolute";
		div.style.background = "transparent";
		div.style.left = "-9999px";
		div.style.top = "-9999px";
		div.style.zIndex = 10000;
		topwin.document.body.appendChild(div);
		div.innerHTML = html_ui;
		div = null;
		topwin.$dialog("mc-dialog-box-"+this.ID).DialogInstance = this;
		topwin.$dialog("mc-dialog-box-"+this.ID).focus();
		//内嵌iframe
		this.Frame = topwin.$dialog("mc-dialog-frame-"+this.ID);
		this.Frame.Box = topwin.$dialog("mc-dialog-frame-box-"+this.ID);
		this.Frame.Process = topwin.$dialog("mc-dialog-frame-loading-"+this.ID);
		this.Frame.style.display = "none";
		if(this.Config.frame) {
			var self = this,
			fun = function(){
				self.Frame.Process.style.display = "none";
				self.Frame.style.display = "";
				self.Config.frame.onload();
			};
			Event.add(this.Frame,'load',fun);
			this.Event.push({obj:this.Frame,act:'load',fn:fun});
			if(this.Config.frame.mode == "text") {
				var win = this.Frame.contentWindow;
				var doc = win.document;
				doc.open();
				doc.write("<body oncontextmenu='return false;'></body>") ;
				var arr = [];
				arr.push("<table height='100%' border='0' align='left' cellpadding='10' cellspacing='0'>");
				arr.push("<tr>");
				if(this.Config.frame.icon && this.Config.frame.icon!="") {
					arr.push("<td align='right'><img id='Icon' src='"+this.Config.frame.icon+"' width='34' height='34' align='absmiddle'></td>");
				}
				arr.push("<td align='left' id='Message' style='font-size:9pt'>"+this.Config.frame.text+"</td></tr></table>");
				var div = doc.createElement("div");
				div.innerHTML = arr.join('');
				doc.body.appendChild(div);
				doc.close();
				div = null;
			} else {
				this.Frame.src = this.Config.frame.url;
			}
			//处理加载提示
			if(this.Config.frame.loading) {
				this.Frame.Process.style.display = "";
			} else {
				this.Frame.Process.style.display = "none";
				this.Frame.style.display = "";
			}
		}
	};
	Dialog.prototype.show = function(){
		this.create();
		var mask = Dialog.getMask(),
		self = this;
		//放入队列
		topwin.Dialog.Queue.push(this.ID);
		//给制定区域赋内容
		this.Title = topwin.$dialog("mc-dialog-title-text-"+this.ID);
		this.setTitle(this.Config.title);
		//--提示栏
		this.Message = {
			Row:topwin.$dialog("mc-dialog-messageRow-"+this.ID),
			Title:topwin.$dialog("mc-dialog-message-title-"+this.ID),
			Text:topwin.$dialog("mc-dialog-message-text-"+this.ID)
		};
		//设置标题栏内容
		this.setMessage(this.Config.message.title,this.Config.message.text);
		if(!this.Config.message.show){
			this.Message.Row.style.display = "none";
		} else {
			this.Message.Row.style.display = "";
		}
		//--按钮栏
		this.Button = {
			Row:topwin.$dialog("mc-dialog-buttonRow-"+this.ID),
			Yes:topwin.$dialog("mc-dialog-button-ok-"+this.ID),
			No:topwin.$dialog("mc-dialog-button-cancel-"+this.ID)
		}
		//设置按钮栏
		this.Button.Yes.innerHTML = this.Config.button.yes.text;
		Event.add(this.Button.Yes,'click',this.Config.button.yes.fn);
		this.Event.push({obj:this.Button.Yes,act:'click',fn:this.Config.button.yes.fn});
		if(!this.Config.button.yes.show) {
			this.Button.Yes.style.display = "none";
		}
		this.Button.No.innerHTML = this.Config.button.no.text;
		this.Button.No.onclick = this.Config.button.no.fn;
		if(!this.Config.button.no.show) {
			this.Button.No.style.display = "none";
		}
		if(!this.Config.button.show) {
			this.Button.Row.style.display = "none";
		}
		//注册拖拽方法
		if(this.Config.drag) { 
			topwin.Drag.init(topwin.$dialog("mc-dialog-drag-handler-"+this.ID),topwin.$dialog("mc-dialog-box-"+this.ID));
		}
		//显示遮罩
		if(this.Config.mask) {
			mask.style.display="";
			fade(mask, 10, 60, 10,function(){});
		}
		//如果按钮栏显示 再将焦点移动到按钮上
		if(this.Config.button.show) {
			if(this.Config.button.yes.show){
				this.Button.Yes.focus();
			} else if(this.Config.button.no.show) {
				this.Button.No.focus();
			}
		}
		//如果已经有弹出层了 将现有的层至于遮罩下
		if(topwin.Dialog.Queue.length>1) {
			for(i=0;i<topwin.Dialog.Queue.length-1;i++) {
				topwin.Dialog.getInstance(topwin.Dialog.Queue[i]).getDialogBox().style.zIndex=4000+i;
			}
		}
		this.setSize(this.Width,this.Height);
		mask = null;
	};
	Dialog.prototype.setTitle = function(str) {
		if(!this.Title) {
			alert("当前对象尚未生成");
			return false;
		}
		this.Title.innerHTML = str;
	};
	Dialog.prototype.setMessage = function(title,text) {
		if(!this.Message) {
			alert("当前对象尚未生成");
			return false;
		}
		this.Message.Title.innerHTML = title;
		this.Message.Text.innerHTML = text;
	};
	Dialog.prototype.setSize = function(w,h) {
		if(!this.Frame) {
			alert("当前对象尚未生成");
			return false;
		}
		var self = this.getDialogBox();
		self.style.width = w + "px";
		self.style.height = h + "px";
		var fh = h - 26;
		if(this.Config.message.show) fh = fh - 50;
		var fw = w - 26;
		if(this.Config.button.show) fh = fh - 40;
		this.Frame.style.width = fw + "px";
		this.Frame.style.height = fh + "px";
		this.Frame.Box.style.width = this.Frame.Process.style.width = fw + "px";
		this.Frame.Box.style.height = this.Frame.Process.style.height = fh + "px";
		this.setPosition();
	};
	Dialog.prototype.setText = function(ico,str) {
		if(!this.Frame) {
			alert("当前对象尚未生成");
			return false;
		}
		this.Config.frame.onload = function(){};
		if(this.Config.frame.mode == "text") {
			var win = this.Frame.contentWindow;
			var doc = win.document;
			if(ico && icon!="") doc.getElementById("Icon").src = ico;
			doc.getElementById("Message").innerHTML = str;
		}
	};
	Dialog.prototype.setURL = function(str,fn) {
		if(!this.Frame) {
			alert("当前对象尚未生成");
			return false;
		}
		this.Config.frame.onload = function(){};
		this.Config.frame.mode = "url";
		var self = this;
		if(this.Config.frame.mode == "url") {
			for(var i = 0 ; i < self.Event.length ; i++) {
				if(self.Event[i].obj==self.Frame) {
					Event.remove(self.Frame,'load',self.Event[i].fn);
				}
			}
			self.Frame.Process.style.display = "";
			self.Frame.style.display = "none";
			var self = this,
			fun = function(){
				self.Frame.Process.style.display = "none";
				self.Frame.style.display = "";
				if(typeof(fn)=="function") fn();
			};
			Event.add(this.Frame,'load',fun);
			this.Event.push({obj:this.Frame,act:'load',fn:fun})
			this.Frame.src = str;
		}
	};
	Dialog.prototype.setPosition = function() {
		var coords = page(topwin), self = this.getDialogBox();
		self.style.top = parseInt(coords.scrollTop/2+coords.clientHeight/2 - parseInt(self.style.height)/2) - 10 + "px";
		self.style.left = parseInt(coords.scrollLeft/2+coords.clientWidth/2 - parseInt(self.style.width)/2) + "px";
		if(this.Config.drag) topwin.Drag.init(topwin.$dialog("mc-dialog-drag-handler-"+this.ID),topwin.$dialog("mc-dialog-box-"+this.ID));
		coords = null;
		self = null;
	};
	Dialog.getMask = function() {
		//创建遮罩
		if(Dialog.Mask) return Dialog.Mask;
		var __m = topwin.$dialog("mc-dialog-mask"),
		coords = page(topwin);
		if(!__m) {
			__m = topwin.document.createElement("div");
			__m.style.display = "none";
			__m.id = "mc-dialog-mask";
			__m.style.position = "absolute";
			__m.style.zIndex = 5000;
			__m.style.width = coords.clientWidth + "px";
			__m.style.height = coords.clientHeight + "px";
			__m.style.top = "0px";
			__m.style.left = "0px";
			__m.style.background = "#33393C";
			__m.style.filter = "alpha(opacity=10)";
			__m.style.opacity = "0.10";
			__m.innerHTML = "<iframe src='about:blank' allowTransparency='true' frameborder='0' style='border:none;overflow-x:hidden;position:absolute;left:0;top:0;z-index:3000;filter:alpha(opacity=0);opacity:0;' width='"+coords.clientWidth+"' height='"+coords.clientHeight+"'></iframe>"
			topwin.document.body.appendChild(__m);
		}
		Dialog.Mask = __m;
		__m = null;
		coords = null;
		return Dialog.Mask;
	};
	Dialog.prototype.getDialogBox = function () {
		var box = topwin.$dialog("mc-dialog-box-"+this.ID);
		try{ return box; }finally{ box = null; }
	};
	Dialog.prototype.close = function(){
		//从队列中删除
		var self = this.getDialogBox();
		topwin.Dialog.Queue.remove(this.ID); 
		//清除对象 回收内存
		this.Title = null;
		this.Message = null;
		this.Frame = null;
		this.Button = null;
		this.Event.each(function(el){
			Event.remove(el.obj,el.act,el.fn);
		});
		//从dom中删除层 先隐藏再删除
		topwin.$dialog("mc-dialog-box-"+this.ID).style.display = "none";
		topwin.$dialog("mc-dialog-drag-handler-"+this.ID).onmousedown=null;
		topwin.$dialog("mc-dialog-drag-handler-"+this.ID).root=null;
		topwin.document.body.removeChild(topwin.$dialog("mc-dialog-box-"+this.ID));
		//控制焦点和遮罩
		var fn = function(){
			Dialog.Mask.style.zIndex = "5000";
			Dialog.Mask.style.display = "none";
		};
		if(topwin.Dialog.Queue.length==0) { //没有弹出层了 隐藏遮罩
			fade(topwin.$dialog("mc-dialog-mask"), 60, 10, 10,fn);
			Event.remove(topwin.document,"keydown",Dialog.onKeyUp);
			Event.remove(topwin.document,"click",Dialog.setFocus);
			Event.remove(topwin,'resize',Dialog.setPosition);
			Event.remove(topwin,'scroll',Dialog.setPosition);
		} else { 
			//将队列最后的一个层位置设为10000 即当前层可见 同时注册拖拽
			var lastID = topwin.Dialog.Queue[topwin.Dialog.Queue.length-1];
			topwin.Dialog.getInstance(lastID).getDialogBox().style.zIndex=10000;
			if(topwin.Dialog.getInstance(lastID).Config.drag) {
				topwin.Drag.init(topwin.$dialog("mc-dialog-drag-handler-"+lastID),topwin.$dialog("mc-dialog-box-"+lastID));
			}
			if(topwin.Dialog.getInstance(lastID).Config.button.show) {
				if(topwin.$dialog("mc-dialog-button-ok-"+lastID).style.display=="") {
					topwin.$dialog("mc-dialog-button-ok-"+lastID).focus();
				} else if(topwin.$dialog("mc-dialog-button-cancel-"+lastID).style.display=="") {
					topwin.$dialog("mc-dialog-button-cancel-"+lastID).focus();
				}
			} else {
				topwin.$dialog("mc-dialog-drag-handler-"+lastID).focus();
			}
		}
		if(ie) CollectGarbage();
		this.Event = null;
		this.Config = null;
		self.DialogInstance = null;
		self = null;
	};
	Dialog.setFocus = function () {
		if(topwin.Dialog.Queue.length>0) {
			var target = topwin.Dialog.getInstance(topwin.Dialog.Queue[topwin.Dialog.Queue.length-1]);
			if(target.Config.button.show) {
				if(topwin.$dialog("mc-dialog-button-ok-"+target.ID).style.display=="") {
					topwin.$dialog("mc-dialog-button-ok-"+target.ID).focus();
				} else if(topwin.$dialog("mc-dialog-button-cancel-"+target.ID).style.display=="") {
					topwin.$dialog("mc-dialog-button-cancel-"+target.ID).focus();
				}
			} else {
				topwin.$dialog("mc-dialog-drag-handler-"+target.ID).focus();
			}
		}
	};
	Dialog.setPosition = function() {
		var arr = topwin.Dialog.Queue;
		if(arr == null || arr.length == 0) return;	
		for(var i=0;i<arr.length;i++){
			topwin.$dialog("mc-dialog-box-"+arr[i]).DialogInstance.setPosition();
		}
	};
	
	Dialog.getInstance = function(id){
		var f = topwin.$dialog("mc-dialog-box-"+id);
		if(!f){ return null;}
		return f.DialogInstance;
	};
	Dialog.DAlert = function (str) {
		var diag = new Dialog();
		diag.Config = {
			id:"alert",
			skin:"coolblue",
			width:400,
			height:224,
			mask:true,
			drag:true,
			title:"系统提示",
			frame:{
				mode:"text",
				url:"",
				icon:Dialog.Path+"icon_alert.gif",
				text:str,
				loading:false,
				onload:function(){
				}
			},
			message:{
				show:false,
				title:"mc dialog",
				text:"提升用户体验从mc dialog开始"
			},
			button:{
				show:true,
				yes:{
					show:false,
					text:"确定",
					fn:function(){
						diag.close();diag.ready=true;
						if(typeof(func1)=="function") func1();
					}
				},
				no:{
					show:true,
					text:"确定",
					fn:function(){diag.close();diag.ready=true;}
				}
			}
		};
		topwin.Dialog.DAlert.Manager.push(diag);
		if(topwin.Dialog.DAlert.Manager.index(diag)==0){
			diag.show();
		} else {
			var _this = setInterval(function(){
				if(topwin.Dialog.DAlert.Manager.index(diag)-1>=0 && topwin.Dialog.DAlert.Manager[topwin.Dialog.DAlert.Manager.index(diag)-1].ready) {
					diag.show();
					clearInterval(_this);
				}
			},10);
		}
	};
	Dialog.DAlert.Manager = [];
	Dialog.Confirm = function (str,func1,func2) {
		var diag = new Dialog();
		diag.Config = {
			id:"confirm",
			skin:"coolblue",
			width:400,
			height:224,
			mask:true,
			drag:true,
			title:"确认提示",
			frame:{
				mode:"text",
				url:"",
				icon:Dialog.Path+"icon_query.gif",
				text:str,
				loading:false,
				onload:function(){
				}
			},
			message:{
				show:false,
				title:"mc dialog",
				text:"提升用户体验从mc dialog开始"
			},
			button:{
				show:true,
				yes:{
					show:true,
					text:"确定",
					fn:function(){
						diag.close();diag.ready=true;
						if(typeof(func1)=="function") func1();
					}
				},
				no:{
					show:true,
					text:"取消",
					fn:function(){
						diag.close();diag.ready=true;
						if(typeof(func2)=="function") func2();
					}
				}
			}
		};
		topwin.Dialog.Confirm.Manager.push(diag);
		if(topwin.Dialog.Confirm.Manager.index(diag)==0){
			diag.show();
		} else {
			var _this = setInterval(function(){
				if(topwin.Dialog.Confirm.Manager[topwin.Dialog.Confirm.Manager.index(diag)-1].ready) {
					diag.show();
					clearInterval(_this);
				}
			},10);
		}
	};
	Dialog.Confirm.Manager = [];
	Dialog.onKeyUp = function(event){
		if(event.keyCode==9){
			if(topwin.Dialog.Queue.length>0){
				Event.stop(event);
			}
		}
		if(event.keyCode==27){//esc
			if(topwin.Dialog.Queue.length!=0){
				var diag = topwin.Dialog.getInstance(topwin.Dialog.Queue[topwin.Dialog.Queue.length-1]);
				diag.Button.No.onclick();
			} else {
				Event.stop(event);
			}
		}
		if(event.keyCode==13){//enter
			if(topwin.Dialog.Queue.length!=0){
				var diag = topwin.Dialog.getInstance(topwin.Dialog.Queue[topwin.Dialog.Queue.length-1]);
				if(diag.Button.Yes.style.display=="") {
					if(typeof(diag.Button.Yes.onclick)=="function") diag.Button.Yes.focus();
				} else if(diag.Button.No.style.display=="") {
					if(typeof(diag.Button.No.onclick)=="function") diag.Button.No.focus();
				}
			} else {
				Event.stop(event);
			}
		}
		if(event.ctrlKey && event.keyCode == 65) {
			Event.stop(event);
		}
	};
	this.Drag = Drag = {
		target:null,
		relate:[0,0],
		init:function(handle,dragMain) {
			Drag.target = handle;
			Drag.target.style.cursor="move";
			Drag.target.root = dragMain; 
			Drag.relate = [0,0];
			Drag.target.onmousedown = Drag.start;
		},
		start:function() {
			var handle = Drag.target,
			id = handle.id.replace("mc-dialog-drag-handler-","");
			Drag.mouseX = topwin.event.clientX;
			Drag.mouseY = topwin.event.clientY;
			Drag.flag = true;
			Drag.relate[0] = Drag.mouseX - parseInt(handle.root.style.left);
			Drag.relate[1] = Drag.mouseY - parseInt(handle.root.style.top);
			topwin.$dialog("mc-dialog-frame-box-"+id).style.display = "";
			topwin.document.onmousemove = Drag.move;
			topwin.document.onmouseup = Drag.end;
			handle = null;
			return false;
		},
		move:function() {
			if(Drag.flag) { 
				var isL = opera?(topwin.event.button==0?true:false):((topwin.event.button==1 || topwin.event.which==1)?true:false);
				if(isL) {
					var handle = Drag.target;
					handle.root.style.cursor="move";
					var mouseX = topwin.event.clientX;
					var mouseY = topwin.event.clientY;
					var top = parseInt(handle.root.style.top);
					var left = parseInt(handle.root.style.left);
					if(ie) handle.setCapture();
					if(gecko) event.preventDefault(); 
					var cl = mouseX - Drag.relate[0];
					var ct = mouseY - Drag.relate[1];
					//判断是否将对象拖出了可视范围
					var ww = page(topwin).clientWidth,wh = page(topwin).clientHeight;
					if(cl + parseInt(Drag.relate[0]) > ww) cl = ww - parseInt(Drag.relate[0]);
					if(ct + parseInt(Drag.relate[1]) > wh) ct = wh - parseInt(Drag.relate[1]);
					if(cl < -Drag.relate[0]) cl = -Drag.relate[0];
					if(ct < -Drag.relate[1]) ct = -Drag.relate[1];
					handle.root.style.left = cl + "px";
					handle.root.style.top = ct + "px";
					Drag.mouseX = mouseX;
					Drag.mouseY = mouseY;
					handle = null;
				}
			}
			return false;
		},
		end:function() {
			if(ie){ Drag.target.releaseCapture(); };
			var id = Drag.target.id.replace("mc-dialog-drag-handler-","");
			topwin.$dialog("mc-dialog-frame-box-"+id).style.display = "none";
			Drag.flag = false;
			topwin.document.onmousemove = null;
			topwin.document.onmouseup = null;
			Drag.target.root.style.cursor="default";
		}
	};
	Event.add(window, "load", Dialog.listener);
})();